/*
 * @Author: DUST
 * @Date: 2023-05-16 16:12:38
 * @LastEditors: DUST
 * @LastEditTime: 2023-12-14 10:55:28
 * @FilePath: \business-card-system\admin\src\utils\userTable.ts
 * @Description: 
 * undefined
 */
import timeFormat from "@/utils/time";
import { PageInterFace, SortInterFace } from "../interface";
import { ref } from "vue";
/**
 * 封装表格事件
 * @param emit emit方法
 */

/**
  like("LIKE","模糊查询"),
  desc("DESC","降序"),
  asc("ASC","升序"),
  eq("=","等于"),
  lt("<","小于"),
  ltEq("<=","小于等于"),
  gt(">","大于"),
  gtEq(">=","大于等于"),
  dateEq("=","日期等于"),
  dateGt(">","日期大于"),
  dateGtEq(">=","日期大于等于"),
  dateLt("<","日期小于"),
  dateLtEq("<=","日期小于等于"),
  timeEq("=","时间等于"),
  timeGt(">","时间大于"),
  timeGtEq(">=","时间大于等于"),
  timeLt("<","时间小于"),
  timeLtEq("<=","时间小于等于"),
  isNull(" IS NULL ","为空"),
  isNotNull(" IS NOT NULL ","不为空"),
 */

const useTable = (emit: any, props: any) => {
  const rowclass = (_: any, index: number) => {
    if (index % 2 == 1) {
      return "rowclass";
    }
    return "";
  };
  const rowskeys = ref<(string | number)[]>([]);
  const lastSelectedRowKeys = ref<(string | number)[]>([]);
  const isSamePage = ref(true);
  const lastPage = ref(0);
  //选择改变
  const onSelectChange = (
    selectedRowKeys: (string | number)[],
    selectedRows: (string | number)[]
  ) => {
    console.log(rowskeys.value);
    console.log("selectedRowKeys:", selectedRowKeys);
    console.log("lastSelectedRowKeys:", lastSelectedRowKeys.value);

    // console.log(rowskeys.value);
    console.log("selectedRows: ${selectedRows}", selectedRows);
    emit("update:rowskeys", selectedRowKeys);
    emit("update:selectedRows", selectedRows);
    // emit("update:selectedRowKeys", selectedRowKeys);
  };
  // 自定义form的change事件
  const changeKeys = (obj: any) => {
    console.log("changekeys params:", obj);
    if (obj.column.filterOperator) {
      if (obj.column.filterOperator == "dateArray") {
        console.log(obj.str[0]);
        obj.setSelectedKeys(
          obj.str.length > 0
            ? [
                Number(new Date(obj.str[0])) +
                  "~" +
                  Number(new Date(obj.str[1])) +
                  "@" +
                  obj.column.filterOperator
              ]
            : []
        );
      } else {
        obj.setSelectedKeys(
          obj.e.target.value
            ? [
                obj.e.target.value +
                  "@" +
                  obj.column.filterOperator +
                  "@" +
                  (obj.column.extra ? obj.column.extra : "")
              ]
            : []
        );
      }
    } else obj.setSelectedKeys(obj.e.target.value ? [obj.e.target.value] : []);
  };

  const handleReset = (
    clearFilters: any,
    column: any,
    inputValue: any,
    confirm: () => void
  ) => {
    clearFilters();
    if (inputValue[column.dataIndex] instanceof Array)
      inputValue[column.dataIndex] = [];
    else inputValue[column.dataIndex] = undefined;

    confirm();
  };
  //分页改变
  const handleTableChange = (
    pagination: PageInterFace,
    filters: any,
    sorter: SortInterFace | SortInterFace[]
  ) => {
    console.log(pagination);
    if (lastPage.value != pagination.current) {
      isSamePage.value = false;
      lastPage.value = pagination.current as any;
      lastSelectedRowKeys.value = [];
    } else {
      isSamePage.value = true;
    }
    const obj: any = {
      ...pagination,
      pageNumber: pagination.current,
      showTotal: (total: any) => `共${total}条数据`,
      sortJson: undefined,
      screenJson: undefined,
      screeningJson: pagination.screeningJson || undefined
    };
    emit("update:pagination", obj);
  };
  //编辑
  const edit = (record: any) => {
    emit("edit", record);
  };
  //删除
  const del = (record: any) => {
    emit("del", record);
  };
  //授权
  const empower = (record: any) => {
    emit("empower", record);
  };
  //取消授权
  const cancel = (record: any) => {
    emit("cancel", record);
  };
  //会议列表的管理
  const manage = (record: any) => {
    emit("manage", record);
  };
  //会议列表统计
  const count = (record: any) => {
    emit("count", record);
  };
  //预约
  //会议列表统计
  const order = (record: any) => {
    emit("order", record);
  };
  //选择
  const select = (record: any) => {
    emit("select", record);
  };
  //换宿舍
  const change = (record: any) => {
    emit("change", record);
  };

  //类型切换
  const typeChange = (record: any) => {
    emit("typeChange", record);
  };
  //同意
  const agree = (record: any) => {
    emit("agree", record);
  };
  //拒绝
  const refuse = (record: any) => {
    emit("refuse", record);
  };

  const look = (record: any) => {
    console.log("查看", record);
    emit("look", record);
  };
  const changeFace = (record: any) => {
    console.log("查看", record);
    emit("changeFace", record);
  };
  const changeCardFace = (record: any) => {
    console.log("查看", record);
    emit("changeCardFace", record);
  };
  //点击行
  const customRow = (record: any) => {
    return {
      onClick: (e: { path: Element[] }) => {
        const elearr = document.getElementsByClassName("selectRow");
        if (elearr.length > 0) {
          elearr[0].classList.remove("selectRow");
        }
        e.path[1].classList.add("selectRow");
        emit("clickRow", record);
      }
    };
  };
  return {
    changeKeys,
    handleReset,
    rowclass,
    onSelectChange,
    handleTableChange,
    edit,
    cancel,
    empower,
    del,
    count,
    manage,
    customRow,
    order,
    select,
    change,
    typeChange,
    agree,
    refuse,
    look,
    changeFace,
    changeCardFace
  };
};

function mergeArray(arr1: any[], arr2: any[]) {
  let _arr = [];
  for (let i = 0; i < arr1.length; i++) {
    _arr.push(arr1[i]);
  }
  for (let x = 0; x < arr2.length; x++) {
    let flag = true;
    for (let j = 0; j < arr1.length; j++) {
      if (arr2[x] === arr1[j]) {
        flag = false;
        break;
      }
    }
    if (flag) {
      _arr.push(arr2[x]);
    }
  }
  _arr = spliceArray(_arr, arr2);
  return _arr;
}
function spliceArray(arr1: any, arr2: any) {
  const _arr = [];
  for (let x = 0; x < arr1.length; x++) {
    for (let j = 0; j < arr2.length; j++) {
      if (arr1[x] === arr2[j]) {
        _arr.push(arr1[x]);
      }
    }
  }
  return _arr;
}

export default useTable;
